﻿/**  版本信息模板在安装目录下，可自行修改。
* Hoggery.cs
*
* 功 能： N/A
* 类 名： Hoggery
* ───────────────────────────────────
* V0.01  2014/4/29 9:55:48   
*/
using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using OnlineManagement.IDAL;
using OnlineManagement.DBUtility;//Please add references
namespace OnlineManagement.SQLServerDAL.Dal
{
	/// <summary>
	/// 数据访问类:Hoggery
	/// </summary>
	public partial class Hoggery:IHoggery
	{
		public Hoggery()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("HoggeryID", "Hoggery"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int HoggeryID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from Hoggery");
			strSql.Append(" where HoggeryID=@HoggeryID");
			SqlParameter[] parameters = {
					new SqlParameter("@HoggeryID", SqlDbType.Int,4)
			};
			parameters[0].Value = HoggeryID;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(OnlineManagement.Model.Hoggery model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into Hoggery(");
			strSql.Append("HoggeryManager,ViceFarm,Feeder,HoggeryAddress,PhoneNO)");
			strSql.Append(" values (");
			strSql.Append("@HoggeryManager,@ViceFarm,@Feeder,@HoggeryAddress,@PhoneNO)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@HoggeryManager", SqlDbType.NVarChar,20),
					new SqlParameter("@ViceFarm", SqlDbType.NVarChar,20),
					new SqlParameter("@Feeder", SqlDbType.NVarChar,20),
					new SqlParameter("@HoggeryAddress", SqlDbType.NVarChar,200),
					new SqlParameter("@PhoneNO", SqlDbType.NVarChar,20)};
			parameters[0].Value = model.HoggeryManager;
			parameters[1].Value = model.ViceFarm;
			parameters[2].Value = model.Feeder;
			parameters[3].Value = model.HoggeryAddress;
			parameters[4].Value = model.PhoneNO;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(OnlineManagement.Model.Hoggery model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update Hoggery set ");
			strSql.Append("HoggeryManager=@HoggeryManager,");
			strSql.Append("ViceFarm=@ViceFarm,");
			strSql.Append("Feeder=@Feeder,");
			strSql.Append("HoggeryAddress=@HoggeryAddress,");
			strSql.Append("PhoneNO=@PhoneNO");
			strSql.Append(" where HoggeryID=@HoggeryID");
			SqlParameter[] parameters = {
					new SqlParameter("@HoggeryManager", SqlDbType.NVarChar,20),
					new SqlParameter("@ViceFarm", SqlDbType.NVarChar,20),
					new SqlParameter("@Feeder", SqlDbType.NVarChar,20),
					new SqlParameter("@HoggeryAddress", SqlDbType.NVarChar,200),
					new SqlParameter("@PhoneNO", SqlDbType.NVarChar,20),
					new SqlParameter("@HoggeryID", SqlDbType.Int,4)};
			parameters[0].Value = model.HoggeryManager;
			parameters[1].Value = model.ViceFarm;
			parameters[2].Value = model.Feeder;
			parameters[3].Value = model.HoggeryAddress;
			parameters[4].Value = model.PhoneNO;
			parameters[5].Value = model.HoggeryID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int HoggeryID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Hoggery ");
			strSql.Append(" where HoggeryID=@HoggeryID");
			SqlParameter[] parameters = {
					new SqlParameter("@HoggeryID", SqlDbType.Int,4)
			};
			parameters[0].Value = HoggeryID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string HoggeryIDlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Hoggery ");
			strSql.Append(" where HoggeryID in ("+HoggeryIDlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public OnlineManagement.Model.Hoggery GetModel(int HoggeryID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 HoggeryID,HoggeryManager,ViceFarm,Feeder,HoggeryAddress,PhoneNO from Hoggery ");
			strSql.Append(" where HoggeryID=@HoggeryID");
			SqlParameter[] parameters = {
					new SqlParameter("@HoggeryID", SqlDbType.Int,4)
			};
			parameters[0].Value = HoggeryID;

			OnlineManagement.Model.Hoggery model=new OnlineManagement.Model.Hoggery();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public OnlineManagement.Model.Hoggery DataRowToModel(DataRow row)
		{
			OnlineManagement.Model.Hoggery model=new OnlineManagement.Model.Hoggery();
			if (row != null)
			{
				if(row["HoggeryID"]!=null && row["HoggeryID"].ToString()!="")
				{
					model.HoggeryID=int.Parse(row["HoggeryID"].ToString());
				}
				if(row["HoggeryManager"]!=null)
				{
					model.HoggeryManager=row["HoggeryManager"].ToString();
				}
				if(row["ViceFarm"]!=null)
				{
					model.ViceFarm=row["ViceFarm"].ToString();
				}
				if(row["Feeder"]!=null)
				{
					model.Feeder=row["Feeder"].ToString();
				}
				if(row["HoggeryAddress"]!=null)
				{
					model.HoggeryAddress=row["HoggeryAddress"].ToString();
				}
				if(row["PhoneNO"]!=null)
				{
					model.PhoneNO=row["PhoneNO"].ToString();
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select HoggeryID,HoggeryManager,ViceFarm,Feeder,HoggeryAddress,PhoneNO ");
			strSql.Append(" FROM Hoggery ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" HoggeryID,HoggeryManager,ViceFarm,Feeder,HoggeryAddress,PhoneNO ");
			strSql.Append(" FROM Hoggery ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM Hoggery ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.HoggeryID desc");
			}
			strSql.Append(")AS Row, T.*  from Hoggery T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		#endregion  BasicMethod
		#region  ExtensionMethod

        public string GetHoggeryName(int hoggeryId) {
            string hoggeryName = string.Empty;
            if(hoggeryId==int.MinValue||hoggeryId==0){
                return hoggeryName;
            }
             SqlDataReader reader=null;
            try
            {
                string query = "SELECT HoggeryName FROM Hoggery WHERE HoggeryID=@hoggeryId";
                SqlParameter[] paras = { new SqlParameter("@hoggeryId", SqlDbType.Int, 4) };
                paras[0].Value = hoggeryId;
                reader = DbHelperSQL.ExecuteReader(query, paras);
                if (reader != null)
                {
                    while (reader.Read())
                    {
                        hoggeryName = reader[0].ToString();
                    }
                }
                return hoggeryName;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally { 
                if(reader!=null){
                    reader.Close();
                }
            }
        }

        /// <summary>
        /// 获取猪场 猪管理日志
        /// </summary>
        /// <param name="pigId"></param>
        /// <returns></returns>
        public DataSet GetHoggeryMgrLog(Guid pigId) {
            DataSet ds = new DataSet();
            string proceName = Common.PubProcedures.PigMgrLogProc;
            SqlParameter[] paras = { new SqlParameter("@pigId", SqlDbType.UniqueIdentifier) };
            paras[0].Value = pigId;
            try
            {
                ds = DbHelperSQL.RunProcedure(proceName, paras);
            }catch(Exception ex){
                throw ex;
            }
            return ds;
        }
		#endregion  ExtensionMethod
	}
}

